using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class GUI_Webpage_User : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadRestaurant(drpRestaurant);
            LoadData();
        }
    }



    protected void grvTable_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvTable.EditIndex = -1;
        DeleteBlank();
        LoadData();
    }
    protected void grvTable_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        TableInfo tableInfo = new TableInfo();
        TableBll tableBll = new TableBll();

        tableInfo.TableID = grvTable.DataKeys[e.RowIndex].Value.ToString();

        tableBll.DeleteTable(tableInfo);
        DeleteBlank();
        LoadData();
    }
    protected void grvTable_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvTable.EditIndex = (int)e.NewEditIndex;
        LoadData();
    }
    protected void grvTable_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TableInfo tableInfo = new TableInfo();
        TableBll tableBll = new TableBll();
        RestaurantBll resBll = new RestaurantBll();
        RestaurantInfo resInfo = new RestaurantInfo();
        string price = ((TextBox)grvTable.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        string numberOfPerson = ((TextBox)grvTable.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        string resName = ((TextBox)grvTable.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
        resInfo.ResName = resName;

        tableInfo.TableID = ((TextBox)grvTable.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
        tableInfo.NumberOfPerson = Int32.Parse(numberOfPerson);
        tableInfo.Price = Int32.Parse(price);
        tableInfo.ResID = resBll.ListResID(resInfo);
        tableInfo.TableDescription = ((TextBox)grvTable.Rows[e.RowIndex].Cells[4].Controls[0]).Text;

        grvTable.EditIndex = -1;

        tableBll.UpdateTable(tableInfo);
        DeleteBlank();
        LoadData();
    }
    protected void btnCreate_Click(object sender, EventArgs e)
    {
        TableInfo tableInfo = new TableInfo();
        TableBll tableBll = new TableBll();
        if (txtTableID.Text != "" && txtNumberSeat.Text != "" && txtPrice.Text != "" &&
            drpRestaurant.Text != "" && filePicture.FileName != "" && filePicture.FileName != "")
        {
            tableInfo.TableID = txtTableID.Text;
            tableInfo.NumberOfPerson = Int32.Parse(txtNumberSeat.Text);
            tableInfo.Price = Int32.Parse(txtPrice.Text);
            tableInfo.ResID = drpRestaurant.Text;
            tableInfo.TablePicture = filePicture.FileName;
            tableInfo.TableDescription = txtDescription.Text;

            if (tableBll.CreateTable(tableInfo) > 0)
            {
                LoadData();
                DeleteBlank();
            }
        }
    }

    private void DeleteBlank()
    {
        txtTableID.Text = "";
        txtNumberSeat.Text = "";
        txtPrice.Text = "";
        //drpRestaurant.Text = "";
        //filePicture.f
        txtDescription.Text = "";
    }


    #region Load restaurant to dropdownlist
    private void LoadRestaurant(DropDownList drpRestaurant)
    {
        RestaurantBll resBll = new RestaurantBll();
        resBll.LoadRestaurant(drpRestaurant);
    }
    #endregion

    #region Load all table to gridview
    private void LoadData()
    {
        TableBll tabBll = new TableBll();
        try
        {
            SqlDataReader reader = tabBll.ListAllTable();
            grvTable.DataSource = reader;
            grvTable.DataBind();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    #endregion
}
